package net.ib.mtalk.ui.buddy.suggestion;

import com.google.protobuf.micro.InvalidProtocolBufferMicroException;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.BuddyInfo;
import net.ib.mntalk.protocol.Common.Name;
import net.ib.mntalk.protocol.Common.SuggestionReasonKnowsOption;
import net.ib.mntalk.protocol.Common.SuggestionReasonMutualOption;
import net.ib.mntalk.protocol.Common.UserProfile;
import net.ib.mntalk.protocol.Session.AddBuddyReq;
import net.ib.mtalk.R;
import net.ib.mtalk.database.DatabaseContract.SuggestionTable;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.MTalkLogWriter;
import net.ib.mtalk.util.ProfileImageLoader;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BuddySuggestionCursorAdapter extends CursorAdapter{

	private int mLayout;
	private LayoutInflater mInflater;
	private BuddySuggestionController mUiController;
	
	public BuddySuggestionCursorAdapter(Context context, Cursor cursor, int flags, BuddySuggestionController uiController) {
		super(context, cursor, flags);
		mLayout = R.layout.list_item_buddy_suggestion;
		this.mUiController = uiController;
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		mInflater = LayoutInflater.from(context);
		View v = mInflater.inflate(mLayout, parent, false);
		
		ViewHolder holder = new ViewHolder();
		holder.rlayout = (RelativeLayout) v.findViewById(R.id.rlayoutSuggestion);
		holder.iv_profile = (ImageView) v.findViewById(R.id.item_bs_profile);
		holder.btn_add = (Button) v.findViewById(R.id.item_bs_add);
		holder.tx_name = (TextView) v.findViewById(R.id.item_bs_name);
		holder.tx_info = (TextView) v.findViewById(R.id.item_bs_info);
		
		v.setTag(holder);
		return v;
	}
	
	private static class ViewHolder {
		RelativeLayout rlayout;
		ImageView iv_profile;
		Button btn_add;
		TextView tx_name;
		TextView tx_info;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		ViewHolder holder = (ViewHolder)view.getTag();
		SuggestionReasonKnowsOption option = null;
		try {
			option = SuggestionReasonKnowsOption.parseFrom(
								cursor.getBlob(cursor.getColumnIndexOrThrow(SuggestionTable.KEY_KNOWS_OPTIONS)));
		} catch (InvalidProtocolBufferMicroException e) {
			e.printStackTrace();
		}
		final SuggestionReasonKnowsOption option1 = option;
		final long msb = cursor.getLong(cursor.getColumnIndex(SuggestionTable.KEY_SUGGESTION_UUID_MSB));
		final long lsb = cursor.getLong(cursor.getColumnIndex(SuggestionTable.KEY_SUGGESTION_UUID_LSB));
		MTalkLogWriter.d(GpbSupport.makeUuid(msb, lsb).toString());
		
		holder.btn_add.setTag(GpbSupport.makeUuidMsg(msb, lsb));
		holder.btn_add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AddBuddyReq addBuddyReq = new AddBuddyReq();
				BuddyInfo info = new Common.BuddyInfo().setUserUuid(option1.getKnowsUuid())
													   .setName(option1.getKnowsProfile().getName())
													   .setProfile(option1.getKnowsProfile())
													   .setBuddyStatus(Common.BUDDY_STATUS_NORMAL);
				addBuddyReq.setBuddy(info);
				mUiController.addBuddy(addBuddyReq);
			}
		});
		if(option != null){
			UserProfile profile = option.getKnowsProfile();
			if(profile != null){
				if(profile.getName() != null){
				}
				holder.tx_name.setText(profile.getName().getDisplayName());
				ProfileImageLoader.getInstance(mContext).loadProfileImage(holder.iv_profile, 
						GpbSupport.UuidMsgToString(GpbSupport.makeUuidMsg(msb, lsb)));
			}
		}
	}
}
